package com.dzt.androidframework.network.mobapi.lottery;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;

import com.dzt.androidframework.R;
import com.dzt.androidframework.activity.TitleFrameActivity;
import com.dzt.androidframework.databinding.ActivityLotteryBinding;

import java.util.ArrayList;
import java.util.List;

public class LotteryActivity extends TitleFrameActivity<ActivityLotteryBinding>
		implements LotteryView {
	private LotteryPresenter presenter;
	private LotteryAdapter adapter;
	private List<String> list = new ArrayList<>();

	@Override
	protected void initData(Bundle savedInstanceState) {
		presenter = new LotteryPresenter();
		presenter.onCreate();
		presenter.attachView(this);
	}

	@Override
	protected String[] initPermissions() {
		return new String[0];
	}

	@Override
	protected int getLayoutId() {
		return R.layout.activity_lottery;
	}

	@Override
	protected void initWidgets() {
		setTitle("彩票查询");
		adapter = new LotteryAdapter(context, list);
		bindingView.lvType.setAdapter(adapter);
		bindingView.lvType.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
				String name = list.get(i);
				presenter.queryLottery(name);
			}
		});
		presenter.queryLotteryList();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		presenter.onDestroy();
	}

	@Override
	public void onSuccess(Lottery lottery) {
		bindingView.tvLottery.setText(lottery.toString());
	}

	@Override
	public void onSuccess(LotteryType lotteryType) {
		list.clear();
		list.addAll(lotteryType.getResult());
		adapter.notifyDataSetChanged();
	}

	@Override
	public void onError(String result) {
		Toast.makeText(LotteryActivity.this, result, Toast.LENGTH_SHORT).show();
	}
}
